﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using Sealight.Core.DataAdapter;
using System.Globalization;

namespace Sealight.UI.WebUI.Areas.Admin.Controllers
{
    [Sealight.UI.WebUI.UIHelpers.SessionExpireFilter]
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public class OrderController : Controller
    {
        #region Template
        public ActionResult Index()
        {
            return View("_SearchOrder");
        }

        public ActionResult OrderDetail(string id)
        {
            Guid order_id = Guid.Empty;
            IList<order_product_variant> data = null;

            Guid.TryParse(id, out order_id);
            if (order_id!= Guid.Empty)
            {
                data = Services.OrderService.GetOrderVariant(order_id);
            }
            return View("_OrderDetail", data);
        }

        #endregion

        #region Ajax
        [GridAction]
        public ActionResult _SearchOrderAjax(string keyword, string from, string to)
        {          
            DateTime? fromDate = null, toDate = null;
            ViewData["keyword"] = keyword;
            ViewData["fromDate"] = from;
            ViewData["toDate"] = to;
            if (!string.IsNullOrEmpty(from))
            {
                fromDate = DateTime.ParseExact(from, "dd/MM/yyyy", CultureInfo.CurrentCulture);              
            }
            if (!string.IsNullOrEmpty(to))
            {
                toDate = DateTime.ParseExact(to, "dd/MM/yyyy", CultureInfo.CurrentCulture);
            }

            var data = Services.OrderService.SearchOrder(keyword, fromDate, toDate);

            if(data==null)
                data = new List<order>();

            return View(new GridModel(data));
        }
        #endregion

    }
}
